from django.db import models
from django.utils.translation import ugettext_lazy as _
from django.db.models import permalink
from django.contrib.auth.models import User
from pennypress.apps.categories.models import CategoryBase, SectionBase
from pennypress.base.models import AuthorTimeCommentBase

class Blog(SectionBase):
    """A blog that will have Posts related to it"""
    title = models.CharField(_('title'), max_length=200)
    description = models.TextField(_('description'))
    
    class Meta:
        verbose_name = _('Blog')
        verbose_name_plural = _('Blogs')

class Post(CategoryBase, AuthorTimeCommentBase):
    blog = models.ForeignKey(Blog, verbose_name=_('Blog'))
    title = models.CharField(_('title'), max_length=200)
    # Anticipate NFA merge
    slug = models.SlugField(_('slug'), prepopulate_from=('title',))
    author = models.ForeignKey(User, blank=True, null=True)
    body = models.TextField(_('body'))
    tease = models.TextField(_('tease'), blank=True)

    class Meta(AuthorTimeCommentBase.Meta):
        verbose_name = _('Post')
        verbose_name_plural = _('Posts')

    class Admin:
        list_display = ('title', 'publish_date', 'status')
        list_filter = ('publish_date', 'categories', 'status')
        search_fields = ('title', 'body')

    def __unicode__(self):
        return u'%s' % self.title

    def get_absolute_url(self):
        return ('blog_detail', (), {
            'year': self.publish_date.year,
            'month': self.publish_date.strftime('%b').lower(),
            'day': self.publish_date.day,
            'slug': self.slug
        })
    get_absolute_url = permalink(get_absolute_url)
